﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuadraticEquation
{
    public class QuadraticEquationSolver
    {
        /// <summary>
        /// Solves a quadratic equation of the type a*x^2+. If there is no real solution an exception is thrown.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="c"></param>
        /// <param name="x1"></param>
        /// <param name="x2"></param>
        /// <exception cref="DivideByZeroException">Thrown when no real solutions were found</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when a is 0</exception>
        public static void SolveEquation(double a, double b, double c, out double x1, out double x2)
        {
            if (a==0)
            {
                throw new ArgumentOutOfRangeException("Not a quadratic equation");
            }
            double determinant = b * b - 4 * a * c;
            if (determinant > 0)
            {
                x1 = (-b + System.Math.Sqrt(determinant)) / (2 * a);
                x2 = (-b - System.Math.Sqrt(determinant)) / (2 * a);
            }
            else if (determinant < 0)
            {
                throw new DivideByZeroException("No real solutions");
            }
            else
            {
                x1 = (-b + System.Math.Sqrt(determinant)) / (2 * a);
                x2 = x1;
            }
        }
    }
}
